package GDoc::Job::Fetch::US::Exec::En::OE::EAD;

use Moose;

use GDoc::Job;
use File::Temp;

extends 'GDoc::Job::Fetch';

my @MONTHS = qw{January February March April May June July August September October November December};

sub setup { 
    my $self = shift;
    $self->_baseurl( 'http://www.oe.netl.doe.gov/ead/ead_archive.aspx' );
    $self->_basedir( 'http://www.oe.netl.doe.gov/ead/' );
}

sub run { 
    my $self = shift;

    if ( $self->data->{index} ) { 
        $self->_fetch_index;
    } elsif ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );

        # get only 2006 to present for now since earlier ones are in .doc format 
        my @links = $mech->find_all_links( text_regex => qr{^\w+\s200[6789]} );
        foreach my $idx( @links ) { 
            my $job = GDoc::Job->new( class => 'Fetch::US::Exec::En::OE::EAD',
                                      queue => $self->fetch_queue,
                                      data  => { index => $idx->url }
                                    );

            $job->submit;
        }
    }
}

sub _fetch_index { 
    my $self = shift;

    my $url = $self->_basedir . $self->data->{index};
    $self->logger->info( "fetching index " . $url );

    my $mech = $self->_mech;
    $mech->get( $url );

    my @links = $mech->find_all_links( url_regex => qr{ead\d{6}\.pdf$} );
    foreach my $link( @links ) { 
        my $url = $link->url;
        $url =~ m{(?<month>\d{2})(?<day>\d{2})(?<year>\d{2})\.pdf$};
        my ( $month, $day, $year ) = @+{ 'month', 'day', 'year' };
        $year += 2000;    # wooo y2k

        my $job = GDoc::Job->new( class => 'Fetch::US::Exec::En::OE::EAD',
                                  queue => $self->fetch_queue,
                                  data  => { link     => $url,
                                             month    => $month,
                                             day      => $day,
                                             year     => $year 
                                           }
                                );

        $job->submit;
    }
}

sub _fetch_pdf { 
    my $self = shift;

    my %data = %{ $self->data };

    my $ident = sprintf( 'OE-EAD-%04d-%02d-%02d', @data{ 'year', 'month', 'day' } );
    my $filename = $ident . '.pdf';
    my $key = 'us/exec/en/oe/' . $filename;

    $self->logger->info( "Storing $data{link} --> $key" );

    my $mech = $self->_mech;
    my $tmpf = File::Temp->new;

    $mech->get( $self->_basedir . $data{link}, ':content_file' => $tmpf->filename );

    my $mname = $MONTHS[ $data{month} - 1 ];
    $self->store_pdf( key      => $key,
                      datafile => $tmpf,
                      filename => $filename,
                      meta     => { description   => sprintf( 'Energy Assurance Daily, %s %s, %04d', $mname, @data{ 'day', 'year' } ),
                                    identifier    => $ident,
                                  }
                    );

}



1;
